package com.songlanyun.common.utils;


import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

/**
 * 地图工具类
 */
public class MapTools {


    /**
     * 计算经纬度
     */
    public static double calculate(String lon1, String lat1, String lon2, String lat2) {

        double meter1 = 0D;
        try {
            // //121.717594,31.12055    121.817629,31.090867
            GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(lat1), Double.parseDouble(lon1));
            GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(lat2), Double.parseDouble(lon2));
            meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
            return meter1;
        } catch (Exception e) {
            return meter1;
        }



    }

    private static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo,
                                           Ellipsoid ellipsoid) {
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);
        return geoCurve.getEllipsoidalDistance();
    }
}
